<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
//use think\console\input\Argument;
//use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use \Exception as E;
/**自动评价
 * Class AutomaticDelivery
 * @package app\command
 */
class AutomaticReply extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('AutomaticReply')
            ->setDescription('the AutomaticReply command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            // 获取自动收货时间
            $time = Db::table('eb_config')->where('name', 'automatic_delivery')->value('value');
            if ($time === null) exit;
            // 自动收货(系统评价)
            $map = '`status` = 4 AND `is_pay` = 1 AND `arrive_time` <= ' . (time() - $time * 60);
            $page = 1;
            $limit = 1000;
            while (true) {
                $data = Db::table('order')->whereRaw($map)->field('admin_id,id,goods_id,user_id')->page($page, $limit)->select()->toArray();
                if (!$data) break;
                $ids = array_column($data, 'id');
                foreach ($data as  $value){
                    $red = Db::table('order_status')->insertGetId([
                        'oid' => $value['id'],
                        'change_message' => '订单已被系统默认评价，如有疑问请联系管理员',
                        'name' => '生鲜系统',
                        'phone' => '15583730896',
                        'role_type' => '系统管理员',
                        'create_time' => time(),
                    ]);
                    $inser = Db::table('goods_reply')->insertGetId([
                        'uid'=>$value['user_id'],
                        'oid'=>$value['id'],
                        'goods_id'=>$value['goods_id'],
                        'admin_id'=>$value['admin_id'],
                        'goods_score'=>5,
                        'service_score'=>5,
                        'comment'=>'系统默认评价',
                        'pics'=>'',
                        'add_time'=>time(),
                    ]);
                }
                $rtn  = Db::table('order')->where('id', 'in', $ids)->update(['status' => 5]);
                if (!$rtn) break;
                $page++;
            }
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }
}
